package com.zmm.account;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


import lombok.extern.slf4j.Slf4j;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class AccountApplication {
	public static void main(String[] args) {
		SpringApplication.run(AccountApplication.class, args);
	}

	@Slf4j
	@RestController
	static class TestController {

		@Autowired
		LoadBalancerClient loadBalancerClient;
		
		@Autowired
		FeginService feginService;

		@GetMapping("/test")
		public String test() {
			// 通过spring cloud common中的负载均衡接口选取服务提供节点实现接口调用
			ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider");
			String url = serviceInstance.getUri() + "/hello?name=" + "didi";
			RestTemplate restTemplate = new RestTemplate();
			String result = restTemplate.getForObject(url, String.class);
			return "Invoke : " + url + ", return : " + result;
		}
		@GetMapping("/test1")
		public String test1() {
			String hello = feginService.hello("哈哈");
			return hello;
		}
	}
}
